<template>
  <div class="hls-video" @click="selectVideo" :class="{'select': videoChoose && bindClick}">
    <div v-if="!videoSrc" class="no-video">
      暂未播放视频
    </div>
    <!-- <Play :playerIndex="playerIndex"  v-else></Play> -->
    <Play
      v-else
      :options="playerOptions"
      :playsinline="true"
      :id="'playerIndex'+playerIndex"
      @loadeddata="onPlayerLoadeddata($event)"
      @canplay="onPlayerCanplay($event)"
      @canplaythrough="onPlayerCanplaythrough($event)"
      @play="onPlayerPlay($event)"
      @waiting="onPlayerWaiting($event)"
      @playing="onPlayerPlaying($event)"
      @ended="onPlayerEnded($event)"
      @error="onPlayerError($event)"
      @ready="onPlayerReady()"
    >
    <!-- @timeupdate="onPlayerTimeupdate($event)"
    @statechanged="playerStateChanged($event)" -->
    </Play>
  </div>
</template>
<script>
import Play from './play'

export default {
  props: {
    bindClick: {
      type: [Boolean],
      default: false
    },
    playerIndex: {
      type: [Number],
      default: 0
    }
  },
  components: {
    Play
  },
  data() {
    return {
      playerOptions: {
        live: true,
        autoplay: true,
        muted: true,
        notSupportedMessage: '此视频暂无法播放，请稍后再试',
        controls: false,
        controlBar: {
          timeDivider: false,
          durationDisplay: false,
          remainingTimeDisplay: false,
          fullscreenToggle: true // 全屏按钮
        },
        sources: [{
          type: 'application/x-mpegURL',
          src: ''
        }]
      },
      videoSrc: '',
      videoChoose: false,
      videoTitle: '',
      videoId: '',
      qplayer: ''
    }
  },
  // watch: {
  //   videoSrc(val, oldval) {
  //     if (this.qplayer) {
  //       this.qplayer.dispose()
  //       this.qplayer = ''
  //     }
  //     if (!this.videoSrc) return
  //     this.$nextTick().then(()=> {
  //       this.qplayer = videojs('playerIndex'+this.playerIndex, this.playerOptions)
  //       this.qplayer.play()
  //     })
  //   }
  // },
  methods: {
    onPlayerLoadeddata(e) {
      console.log('loadeddata')
    },
    onPlayerCanplay(e) {
      console.log('canplay')
    },
    onPlayerCanplaythrough(e) {
      console.log('canplaythrough')
    },
    onPlayerPlay(e) {
      console.log('play')
    },
    onPlayerWaiting(e) {
      console.log('waiting')
    },
    onPlayerPlaying(e) {
      console.log('playing')
    },
    onPlayerEnded(e) {
      console.log('ended')
    },
    onPlayerError(e) {
      console.log('error')
    },
    onPlayerReady() {
      console.log('ready')
    },
    selectVideo() {
      if (this.bindClick) {
        this.videoChoose = !this.videoChoose
        this.$emit('select', this.videoChoose)
      }
    },
    setChoose(val) {
      this.videoChoose = val
    }
  }
}
</script>

<style lang="scss">
  .hls-video {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
    &.select {
      border: 2px solid $color-danger;
    }
    .video-player {
      width: 100%;
      height: 100%;
    }
    .has-video{
      width: 100%;
      height: 100%;
    }
    .video-js {
      height: 100%;
      width: 100%;
      outline: none !important;
      &.vjs-playing .vjs-tech {
        pointer-events: none;
      }
      .vjs-big-play-button {
        // 播放按钮换成圆形
        height: 2em;
        width: 2em;
        line-height: 2em;
        border-radius: 1em;
      }
      .vjs-control:focus {
        outline: none !important;
      }
      .vjs-control-bar {
        .vjs-button > .vjs-icon-placeholder:before {
          line-height: 1;
          font-size: 22px;
        }
        .vjs-fullscreen-control {
          margin-right: 25px;
          margin-top: 2px;
        }
        .vjs-volume-panel {
          position: absolute;
          right: 62px;
          top: 2px;
          .vjs-volume-bar {
            margin: 0.7em 0.45em;
          }
        }
        .vjs-close-control {
          position: absolute;
          right: 0;
          font-weight: bold;
          font-size: 20px;
          width: 42px;
          cursor: pointer;
        }
        .vjs-shot-control {
          position: absolute;
          right: 53px;
          top: -1px;
          font-size: 20px;
          width: 42px;
          cursor: pointer;
        }
        .vjs-video-title {
          position: absolute;
          left: 5px;
          width: calc(100% - 210px);
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          word-break: break-all;
          text-align: left;
        }
      }
      .vjs-error-display {
        background: #000;
        &:before {
          content: ''
        }
        .vjs-modal-dialog-content {
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
    }
    // .vjs-fluid {
    //   padding-top: 0 !important;
    // }
    .no-video {
      background: #000;
      color: #fff;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
</style>